package com.jpx.nacosproducer.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jpx.nacosproducer.config.ResultEntity;
import com.jpx.nacosproducer.dto.OrderDto;
import com.jpx.nacosproducer.entity.*;
import com.jpx.nacosproducer.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-03-16
 */
@RestController
@RequestMapping("/tCustomer")
public class TCustomerController {
    @Autowired
    ITCustomerService itCustomerService;
    @Autowired
    ITTypeService itTypeService;
    @Autowired
    ITMedicService itMedicService;
    @Autowired
    ITDoctorService itDoctorService;
    @Autowired
    ITOrderZhuService itOrderZhuService;
    @Autowired
    ITOrderService itOrderService;
    @Autowired
    ITKeService itKeService;

    @RequestMapping("/customerList")
    public ResultEntity customerList(){
        List<TCustomer> tCustomers = itCustomerService.selectList(new EntityWrapper<TCustomer>());
        return ResultEntity.ok(tCustomers);
    }

    @RequestMapping("/selectCustomerById")
    public ResultEntity selectCustomerById(Integer id){
        TCustomer tCustomer = itCustomerService.selectById(id);
        return ResultEntity.ok(tCustomer);
    }

    @RequestMapping("/getTypeList")
    public ResultEntity getTypeList(){
        List<TType> tTypes = itTypeService.selectList(new EntityWrapper<TType>());
        return ResultEntity.ok(tTypes);
    }

    @RequestMapping("/getDoctorList")
    public ResultEntity getDoctorList(){
        List<TDoctor> tDoctors = itDoctorService.selectList(new EntityWrapper<TDoctor>());
        return ResultEntity.ok(tDoctors);
    }

    @RequestMapping("/selectMedicById")
    public ResultEntity selectMedicById(Integer id){
        List<TMedic> list = itMedicService.selectMedicById(id);
        return ResultEntity.ok(list);
    }

    @RequestMapping("/selectMedicByName")
    public ResultEntity selectMedicByName(String name){
        Wrapper<TMedic> wrapper = new EntityWrapper<TMedic>();
        wrapper.like("name",name);
        List<TMedic> list = itMedicService.selectList(wrapper);
        return ResultEntity.ok(list);
    }

    @RequestMapping("/add")
    public ResultEntity add(TCustomer tCustomer){
        itCustomerService.insertOrUpdate(tCustomer);
        return ResultEntity.ok();
    }

    @RequestMapping("/save")
    @Transactional
    public ResultEntity save(@RequestBody List<OrderDto> RPList, String sumprice,Integer customerId,Integer doctorid,Integer jzstatus){
        TOrderZhu tOrderZhu = new TOrderZhu();
        tOrderZhu.setCustomerId(customerId);
        tOrderZhu.setDoctorId(doctorid);
        tOrderZhu.setSumprice(sumprice);
        tOrderZhu.setState(0);
        tOrderZhu.setJzstatic(jzstatus);
        itOrderZhuService.insert(tOrderZhu);
        Integer oid = itOrderZhuService.selectMaxId();

        List<TOrder> list = new ArrayList<TOrder>();
        for (OrderDto o:RPList) {
            TOrder tOrder = new TOrder();
            tOrder.setTNum(o.getM());
            tOrder.setTSum(o.getM1());
            tOrder.setMedicId(o.getMedicId());
            tOrder.setOid(oid);
            list.add(tOrder);
        }
        itOrderService.insertBatch(list);

        return ResultEntity.ok();
    }

    @RequestMapping("/shou")
    @Transactional
    public ResultEntity shou(){
        Integer id = itOrderZhuService.selectMaxId();
        Map map = new HashMap();
        TOrderZhu tOrderZhu = itOrderZhuService.selectById(id);
        map.put("tOrderZhu",tOrderZhu);
        List list = itOrderService.selectOrderList(id);
        map.put("list",list);
        TCustomer tCustomer = itCustomerService.selectById(tOrderZhu.getCustomerId());
        map.put("tCustomer",tCustomer);
        TDoctor tDoctor = itDoctorService.selectById(tOrderZhu.getDoctorId());
        map.put("tDoctor",tDoctor);
        TKe tKe = itKeService.selectById(tDoctor.getKeid());
        map.put("tKe",tKe);
        return ResultEntity.ok(map);
    }
}
